<?php
##############################################
#
#   用户
# @ Time : 2017-3-1 - 下午11:55:14
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Home\Controller;
use Home\Model\UsersModel;
use Home\Model\ActivityOrderModel;
use Home\Model\ActivityModel;
use Admin\Model\AdminBaseModel;
use Home\Model\CollectModel;

class UsersController extends BaseController{
	private $_users;
	
	public function _initialize(){
	    parent::_initialize();
	    $this->assign('active', 'user');
	    $this->_users = new UsersModel();
	    //是否为会员
	    $user_type = $this->_users->isUsers($this->user_id);
	    if($user_type == 1){
	        self::promptPage('对不起，您还不是正式会员，请绑定手机成为会员！', U("Index/applyUser"), false, '立马绑定');
	    }
	}
	
	/**
	 * 会员中心页
	 */
	public function index(){
	    self::userInfo();
	    //订单统计
	    self::order_count();
	    //主办方最新活动
	    self::newActivity();
	    //我的申请
	    self::my_apply_data();
	    //我的消息
	    self::my_message();
	    $this->display('index');
	}
	
	/**
	 * 用户信息
	 */
	public function userInfo(){
	    $user_data = M('users')->field('user_type,expert_type')->where("id = {$this->user_id}")->find();
	    $this->assign('users', $user_data);
	}
	
	/**
	 * 待付款、待参与、待完成订单总数
	 */
	public function order_count(){
	    $activity_order_model = new ActivityOrderModel();
	    $order_count = $activity_order_model->order_count($this->user_id);
	    $this->assign('order_count', $order_count);
	}
	
	/**
	 * 我的消息
	 */
	public function my_message(){
	    $act_count = M('activity_message')->where("user_id = {$this->user_id} and status = 1")->count();
            $apply_count = M('apply_message')->where("user_id = {$this->user_id} and status = 1")->count();
            $make_count = M('make_message')->where("user_id = {$this->user_id} and status = 1")->count();
	    $discuss_count = M('discuss')->where("reply_user_id = {$this->user_id} and status = 1")->count();
	    $message_count = $act_count + $discuss_count + $apply_count + $make_count;
	    $this->assign('message_count', $message_count);
	}
	
	/**
	  * 用户详情
	 */
	public function usersDetail(){
	    if(IS_POST){
	        $data = I('post.');
	        $result = M('users')->where("id = {$this->user_id}")->save($data);
	    }
	    $data = M('users')->field('nickname,headimgurl,phone_number')->where("id = {$this->user_id}")->find();
	    $this->assign('data', $data);
	    $this->display('user_detail');
	}
	
	/**
	  * 最新活动
	 */
	public function newActivity(){
	    $activity_model = new ActivityModel();
	    $data = $activity_model->newActivity($this->user_id);
	    if(count($data[0]) > 0){
	        $this->assign('new_activity', 1);
	    }
	    $this->assign('new', $data[0]);
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_new_list');
	        exit;
	    }
	}
	
	/**
	 * 发布活动
	 */
	public function releaseActivity(){
	    if(IS_GET){
	        
	       //判断是否已经申请待审核
	       self::is_apply();
	       //是否为会员类型
	       self::is_user_type();
	       
	       $category = M('activity_category')->field('id, name')->where("is_show = 1")->order('sort asc, id desc')->select();
	       $this->assign('category', $category);
	       $this->display('release_activity');
	    }else if(IS_POST){
	        $data = I('post.');
	        if($_FILES['activity_img']['name']){
	            $photo_img = AdminBaseModel::upload(900, 255, 'activity_img/');
	            $data['activity_img'] = $photo_img['activity_img'];  //serialize($photo_img)
	        }
	        $address_arr = explode(' ', $data['address']);
	        $data['province'] = $address_arr[0];
	        $data['city'] = $address_arr[1];
	        $data['area'] = $address_arr[2];
	        $data['start_time'] = strtotime($data['start_time']);
	        $data['end_time'] = strtotime($data['end_time']);
	        $data['type'] = 2; //活动类型 1、官方活动  2、第三方
	        $data['user_id'] = $this->user_id;
	        $data['nickname'] = $this->nickname;
	        $data['trial_status'] = 1; //'活动审核状态  1：待审核 2：已通过 3：不通过'
	        $data['add_time'] = time();
	        $result = M('activity')->add($data);
	        if($result)
	            $this->redirect('show_page', array('id' => $result, 'act'=>'add'));
	        else
	            $this->error('提交失败');
	    }
	}
	
	/**
	 * 修改活动
	 */
	public function editActivity(){
	    if(IS_GET){
	       $activity_id = I('activity_id');
	       $activity_model = new ActivityModel();
	       $data = $activity_model->editActivity($activity_id);
	       $category = M('activity_category')->field('id, name')->where("is_show = 1")->order('sort asc, id desc')->select();
	       $this->assign('category', $category);
	       $this->assign('data', $data);
	       $this->assign('activity_id', $activity_id);
	       $this->display('edit_activity');
	    }else if(IS_POST){
	        $data = I('post.');
	        if($_FILES['activity_img']['name']){
	            $photo_img = AdminBaseModel::upload(900, 255, 'activity_img/');
	            $data['activity_img'] = $photo_img['activity_img'];  //serialize($photo_img)
	        }
	        $address_arr = explode(' ', $data['address']);
	        $data['province'] = $address_arr[0];
	        $data['city'] = $address_arr[1];
	        $data['area'] = $address_arr[2];
	        $data['start_time'] = strtotime($data['start_time']);
	        $data['end_time'] = strtotime($data['end_time']);
	        $data['add_time'] = time();
	        $result = M('activity')->where("id = {$data['activity_id']}")->save($data);
	        if($result)
	            $this->redirect('show_page', array('id' => $data['activity_id'], 'act'=>'edit'));
	        else
	            $this->error('修改失败');
	    }
	}
	
	/**
	 * 判断是否已经申请待审核
	 */
	public function is_apply(){
	    $row = M('apply_user')->where("user_id = {$this->user_id}")->count();
	    if($row){
	        $status = M('apply_user')->where("user_id = {$this->user_id}")->getField('status');
	        if($status != 2){
	            self::promptPage('您已提交申请，资料审核中，请耐心等待！', U("Users/index"), false,'用户中心');
	        }
	    }
	}
	
	/**
	 * 是否为会员类型
	 */
	public function is_user_type(){
	    $user_type = $this->_users->isUsers($this->user_id);
	    if($user_type < 3){
	        self::promptPage('对不起，请申请活动会员类型审核通过后才可发布活动！', U("Index/applyUserType"), false, '立马申请');
	    }
	}
	
	/**
	 * 填写活动简介
	 */
	public function show_page(){
	    if(IS_GET){
	        $id = I('get.id');
	        $act = I('get.act');
	        if($act == 'edit'){
	            $describe = M('activity')->where("id = $id")->getField('describe');
	            $this->assign('describe', $describe);
	        }
	        $this->assign('id', $id);
	        $this->assign('act', $act);
	        $this->display('describe');
	    }elseif(IS_POST){
	        $id = I('post.id');
	        $act = I('post.act');
	        $data['describe'] = I('post.describe');
	        $result = M('activity')->where("id = $id")->save($data);
	        if($result)
	            $this->redirect('submit_success', array('act'=>$act));
	        else 
	            $this->error('提交失败');
	    }
	}
	
	/**
	 * 提交申请成功
	 */
	public function submit_success(){
	   /*  $describe = M('activity')->where("id = 9")->getField('describe');
	    $this->assign('describe', $describe);
	    $this->display('describe'); */
	    $act = I('get.act');
	    if($act == 'add'){
	        $this->assign('title', '提交成功');
	        $this->assign('message', '您的活动申请已提交，我们会以2个工作日内审核！');
	    }else{
	        $this->assign('title', '修改成功');
	        $this->assign('message', '您的活动已修改，我们会以2个工作日内审核！');
	    }
	    $this->assign('jump_url', U("Users/index"));
	    $this->assign('text', '确定');
	    $this->display('Public/publish_tishi');
	}
	
	/**
	 * 活动管理
	 */
	public function activity_administration(){
	    //判断是否已经申请待审核
	    self::is_apply();
	    //是否为会员类型
	    self::is_user_type();
	    //全部活动
	    self::allActivity();
	    //待审核活动
	    self::pendingAuditActivity();
	    //不通过活动
	    self::notPassActivity();
	    //已发布活动
	    self::release();
	    
	    $this->display('user_order');
	}
	
        /**
	 * 预约管理
	 */
    public function user_apply_order(){
        //判断是否已经申请待审核
        //是否为专家类型
        self::is_my_apply();
            //全部预约
	    self::allMake();
	    //待审核预约
	    self::pendingAuditMake();
	    //不通过预约
	    self::notPassMake();
	    //已发布预约
	    self::release_Make();
        $this->display();
    }
    
	/**
	 * 全部活动
	 */
	public function allActivity(){
	    $activity_model = new ActivityModel();
	    $data = $activity_model->allActivity($this->user_id);
	    $this->assign('all', $data[0]);
	    if(count($data[0]) > 0){
	        $this->assign('all_show', 1);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_all_list');
	        exit;
	    }
	}
        /**
	 * 全部预约
	 */
	public function allMake(){
            $count = M('make')->where("user_id = {$this->user_id}")->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 5);
	    $show = $Page->show();
            $all_make = M('make')->where("user_id = {$this->user_id}")->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            foreach ($all_make as $k=>$v){
                $all_make[$k]['select_time'] = explode(',', $v['select_time']);
                $selectTime = $all_make[$k]['select_time'];
                foreach ($selectTime as $kk=>$vv){
                    $weekarray=array("日","一","二","三","四","五","六");
                    $select_time[$kk] = $weekarray[$vv];
                    $select = implode('、', $select_time);
                }
                $all_make[$k]['select_time'] = $select;
                $all_make[$k]['yuyue_count'] = M('make_users')->where('make_id='.$all_make[$k]['id'])->count();
                $all_make[$k]['apply_id'] = M('apply')->where('user_id='.$all_make[$k]['user_id'])->field('id')->find();
            }
	    $this->assign('all_make',$all_make);
	    if(count($all_make) > 0){
	        $this->assign('all_show', 1);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_make_all_list');
	        exit;
	    }
	}
	
	/**
	 * 已发布活动
	 */
	public function release(){
	    $activity_model = new ActivityModel();
	    $data = $activity_model->release($this->user_id);
	    $this->assign('release', $data[0]);
	    if(count($data[0]) > 0){
	        $this->assign('release_show', 1);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_release_list');
	        exit;
	    }
	}
        
        /**
	 * 已发布预约
	 */
	public function release_Make(){
            $count = M('make')->where("user_id = {$this->user_id} and trial_status=2")->count();// 查询满足要求的总记录数
            $Page = new \Think\Page($count, 5);
	    $show = $Page->show();
	    $release_make = M('make')->where("user_id = {$this->user_id} and trial_status=2")->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            foreach ($release_make as $k=>$v){
                $release_make[$k]['select_time'] = explode(',', $v['select_time']);
                $selectTime = $release_make[$k]['select_time'];
                foreach ($selectTime as $kk=>$vv){
                    $weekarray=array("日","一","二","三","四","五","六");
                    $select_time[$kk] = $weekarray[$vv];
                    $select = implode('、', $select_time);
                }
                $release_make[$k]['select_time'] = $select;
                $release_make[$k]['yuyue_count'] = M('make_users')->where('make_id='.$release_make[$k]['id'])->count();
                $release_make[$k]['apply_id'] = M('apply')->where('user_id='.$release_make[$k]['user_id'])->field('id')->find();
            }
	    $this->assign('release_make',$release_make);
	    if(count($release_make) > 0){
	        $this->assign('release_show', 1);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_make_release_list');
	        exit;
	    }
	}
	
	/**
	 * 待审核活动
	 */
	public function pendingAuditActivity(){
	    $activity_model = new ActivityModel();
	    $data = $activity_model->pendingAuditActivity($this->user_id);
	    $this->assign('stay', $data[0]);
	    if(count($data[0]) > 0){
	        $this->assign('stay_show', 1);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_stay_list');
	        exit;
	    }
	}
        
        /**
	 * 待审核预约
	 */
	public function pendingAuditMake(){
            $count = M('make')->where("user_id = {$this->user_id} and trial_status=1")->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 5);
	    $show = $Page->show();
	    $stay_make = M('make')->where("user_id = {$this->user_id} and trial_status=1")->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    foreach ($stay_make as $k=>$v){
                $stay_make[$k]['select_time'] = explode(',', $v['select_time']);
                $selectTime = $stay_make[$k]['select_time'];
                foreach ($selectTime as $kk=>$vv){
                    $weekarray=array("日","一","二","三","四","五","六");
                    $select_time[$kk] = $weekarray[$vv];
                    $select = implode('、', $select_time);
                }
                $stay_make[$k]['select_time'] = $select;
                $stay_make[$k]['yuyue_count'] = M('make_users')->where('make_id='.$stay_make[$k]['id'])->count();
                $stay_make[$k]['apply_id'] = M('apply')->where('user_id='.$stay_make[$k]['user_id'])->field('id')->find();
            }
            $this->assign('stay_make',$stay_make);
	    if(count($stay_make) > 0){
	        $this->assign('stay_show', 1);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_make_stay_list');
	        exit;
	    }
	}
	
	/**
	 * 不通过活动
	 */
	public function notPassActivity(){
	    $activity_model = new ActivityModel();
	    $data = $activity_model->notPassActivity($this->user_id);
	    $this->assign('not', $data[0]);
	    if(count($data[0]) > 0){
	        $this->assign('not_show', 1);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_not_list');
	        exit;
	    }
	}
        
        /**
	 * 不通过预约
	 */
	public function notPassMake(){
            $count = M('make')->where("user_id = {$this->user_id} and trial_status=3")->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 5);
	    $show = $Page->show();
	    $not_make = M('make')->where("user_id = {$this->user_id} and trial_status=3")->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    foreach ($not_make as $k=>$v){
                $not_make[$k]['select_time'] = explode(',', $v['select_time']);
                $selectTime = $not_make[$k]['select_time'];
                foreach ($selectTime as $kk=>$vv){
                    $weekarray=array("日","一","二","三","四","五","六");
                    $select_time[$kk] = $weekarray[$vv];
                    $select = implode('、', $select_time);
                }
                $not_make[$k]['select_time'] = $select;
                $not_make[$k]['yuyue_count'] = M('make_users')->where('make_id='.$not_make[$k]['id'])->count();
                $not_make[$k]['apply_id'] = M('apply')->where('user_id='.$not_make[$k]['user_id'])->field('id')->find();
            }
            $this->assign('not_make',$not_make);
	    if(count($not_make) > 0){
	        $this->assign('not_show', 1);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_make_not_list');
	        exit;
	    }
	}
	
	/**
	 * 我的订单
	 */
	public function my_order(){
	    //全部订单
	    self::all_order();
	    //待付款订单
	    self::stay_order();
	    //待参与订单
	    self::stay_participate_order();
	    //已完成订单
	    self::complete_order();
	    //已取消订单
	    self::cancel_order();
	    $this->display('my_order');
	}
	
	/**
     * 全部订单   
	 */
	public function all_order(){
	    $activity_order_model = new ActivityOrderModel();
	    $data = $activity_order_model->allOrder($this->user_id);
	    $this->assign('all', $data[0]);
	    if(count($data[0]) > 0){
	        $this->assign('all_show', 1);
	        $this->assign('all_count', $data[2]);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_order_all_list');
	        exit;
	    }
	}
	
	/**
	 * 待付款订单
	 */
	public function stay_order(){
	    $activity_order_model = new ActivityOrderModel();
	    $data = $activity_order_model->orderStatus($this->user_id, 1);
	    $this->assign('stay', $data[0]);
	    if(count($data[0]) > 0){
	        $this->assign('stay_show', 1);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_stay_order_list');
	        exit;
	    }
	}
    
	/**
	 * 待参与订单
	 */
	public function stay_participate_order() {
	    $activity_order_model = new ActivityOrderModel();
	    $data = $activity_order_model->orderStatus($this->user_id, 2);
	    $this->assign('stay_participate', $data[0]);
	    if(count($data[0]) > 0){
	        $this->assign('stay_participate_show', 1);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_stay_participate_order_list');
	        exit;
	    }
	}
	
	/**
	 * 已完成订单
	 */
	public function complete_order() {
	    $activity_order_model = new ActivityOrderModel();
	    $data = $activity_order_model->orderStatus($this->user_id, 3);
	    $this->assign('complete', $data[0]);
	    if(count($data[0]) > 0){
	        $this->assign('complete_show', 1);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_complete_order_list');
	        exit;
	    }
	}
	
	/**
	 * 已取消
	 */
	public function cancel_order(){
	    $activity_order_model = new ActivityOrderModel();
	    $data = $activity_order_model->orderStatus($this->user_id, 4);
	    $this->assign('cancel', $data[0]);
	    if(count($data[0]) > 0){
	        $this->assign('cancel_show', 1);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_cancel_order_list');
	        exit;
	    }
	}
	
	/**
	 * 我的关注
	 */
	public function collect(){
        //我关注的活动
	    self::collect_activity();
        //我关注的预约
        self::collect_make();
        $this->display('collect');
            
	}
        
        
     /**
	 * 发布预约
	 */
	public function user_expert_yuyue(){
            $user_id = session('user_id');
            $userInfo = M('apply')->where('user_id='.$user_id)->field('user_name,headimgurl')->find();
            $users = M('users')->where('id='.$user_id)->field('user_type,expert_type')->find();
            if($users['expert_type']==''){
                self::promptPage('对不起，请申请成为专家类型审核通过后才可发布预约！', U("Apply/expert_join"), false, '立马申请');
            }
            if(IS_POST){
                $data = I('post.');
                $pattern = '/^0?(13|14|15|17|18)[0-9]{9}$/';
                
                if(!$data['mobile']){
                    $this->ajaxReturn(array('status'=>2,'message'=>'请填写手机号码'));
                }
                if(!preg_match($pattern,$data['mobile'])){
                    $this->ajaxReturn(array('status'=>2,'message'=>'手机号码格式不正确'));
                }
                if(!$data['select_time']){
                    $this->ajaxReturn(array('status'=>2,'message'=>'请选择时间'));
                }
                if($data['select_time']){
                        $data['select_time'] = implode(',', $data['select_time']);
                }
                $count = M('make')->where('user_id='.$user_id)->max('add_time');
                $monday = strtotime(date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))));
                $sunday = strtotime(date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))));
                if($monday<=$count && $count<=$sunday){
                    $this->ajaxReturn(array('status'=>2,'message'=>'本星期你已经发布过了'));
                }
                if($data['id']){
                    $arr = array(
                        'mobile'=>$data['mobile'],
                        'select_time'=>$data['select_time'],
                        'trial_status'=>1,
                    );
                    $value = M('make')->where('id='.$data['id'])->save($arr);
                    if($value){
                        $this->ajaxReturn(array('status'=>1,'message'=>'修改成功，请耐心等待后台审核'));
                    }  else {
                        $this->ajaxReturn(array('status'=>2,'message'=>'修改失败'));
                    }
                }else{
                    $arr = array(
                        'user_id'=>$user_id,
                        'name'=>$userInfo['user_name'],
                        'mobile'=>$data['mobile'],
                        'select_time'=>$data['select_time'],
                        'head_img'=>$userInfo['headimgurl'],
                        'add_time'=>time(),
                    );
                    $value = M('make')->add($arr);
                    if($value){
                        $this->ajaxReturn(array('status'=>1,'message'=>'发布成功，请耐心等待后台审核'));
                    }  else {
                        $this->ajaxReturn(array('status'=>2,'message'=>'发布失败'));
                    }
                }
                
            }  else {
                if(I('id')){
                    $map['id'] = I('id');
                    $value = M('make')->where($map)->find();
                    if(count($value)>0){
                        $this->assign('status_show',1);
                    }
                    $value['select_time'] = explode(',', $value['select_time']);
                    $this->assign('value',$value);
                }
                $this->display();
            }
	    
	}
        
	
	/**
	 * 我关注的活动
	 */
	public function collect_activity(){
	    $collect_model = new CollectModel();
	    $data = $collect_model->collectActivity($this->user_id);
	    $this->assign('today', time());
	    $this->assign('activity', $data[0]);
	    if(count($data[0]) > 0){
	        $this->assign('activity_show', 1);
	        $this->assign('collect_count', $data[2]);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_collect_activity_list');
	        exit;
	    }
	}
        
        /**
	 * 我关注的预约
	 */
	public function collect_make(){
	   $user_id = session('user_id');
	    $field = array('co.id,co.user_id,co.make_id,co.create_time', 'a.user_name,a.headimgurl,a.phone,a.add_time,a.signature,a.type,a.user_id as apply_user_id,a.id as apply_id');
	    $map['user_id'] = $user_id;
	    $count = M('make_collection')->where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 5);
	    $show = $Page->show();
	    $result = M('make_collection')->alias('co')
	                   ->field($field)
	                   ->join("__APPLY__ as a on a.id = co.make_id")
	                   ->where('co.user_id='.$user_id)
	                   ->order('co.id desc')
	                   ->limit($Page->firstRow.','.$Page->listRows)
	                   ->select();
            $monday = strtotime(date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))));
            $sunday = strtotime(date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))));
            foreach ($result as $key=>$value){
                $result[$key]['status_show'] = M('make')->where('user_id='.$value['apply_user_id'].' and add_time>='.$monday.' and add_time<='.$sunday)->count();
            }
	    $this->assign('make', $result);
	    if(count($result) > 0){
	        $this->assign('make_show', 1);
	        $this->assign('make_collect_count', $count);
	    }
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_collect_make_list');
	        exit;
	    }
	}
        
	/**
	 * 取消收藏
	 */
	public function cancel_collect() {
	    $collect_id = I('collect_id');
	    $collect_model = new CollectModel();
	    $result = $collect_model->cancelCollect($collect_id);
	    if($result)
	        $this->ajaxReturn(array('status'=>1,'message'=>'操作成功'));
	    else
	        $this->ajaxReturn(array('status'=>2,'message'=>'操作失败'));
	}
	
        /**
	 * 取消关注
	 */
	public function cancel_make() {
	    $collect_id = I('collect_id');  
	    $result = M('make_collection')->where('id='.$collect_id)->delete();
	    if($result)
	        $this->ajaxReturn(array('status'=>1,'message'=>'操作成功'));
	    else
	        $this->ajaxReturn(array('status'=>2,'message'=>'操作失败'));
	}
        
	/**
	 * 删除扫码人员
	 */
	public function delete_sweep() {
	    $sweep_id = I('sweep_id');
	    $result = M('sweep')->where("id = $sweep_id")->delete();
	    if($result)
	        $this->ajaxReturn(array('status'=>1,'message'=>'操作成功'));
	    else
	        $this->ajaxReturn(array('status'=>2,'message'=>'操作失败'));
	}
	
	/**
	 * 订单详情
	 */
	public function orderDetail() {
	    $order_id = I('order_id');
	    $activity_order_model = new ActivityOrderModel();
	    $data = $activity_order_model->orderDetail($order_id);
	    $this->assign('activity', $data);
	    $this->display('Order/order_detail');
	}
	
	/**
	 * 电子票
	 */
	public function electronics() {
	    $order_id = I('order_id');
	    $activity_order_model = new ActivityOrderModel();
	    $data = $activity_order_model->getElectronics($order_id);
	    $this->assign('data', $data);
	    $this->display('Order/electronics');
	}
	
	/**
	 * 我的申请
	 */
	public function my_apply() {
	        $apply_data = M('apply_user')->where("user_id = {$this->user_id}")->find();
	        $this->assign('apply', $apply_data);
	        $this->display('my_apply');
	}
	
	/**
	 * 查询我是否已经申请
	 */
	public function my_apply_data(){
	    $apply_count = M('apply_user')->where("user_id = {$this->user_id}")->count();
	    if($apply_count){
	        $this->assign('is_apply', $apply_count);
	        $status = M('apply_user')->where("user_id = {$this->user_id}")->getField('status');
	        if($status == 3){
	            $this->assign('shwo_apply_message', 1);
	        }
	    }
	}
        
        /**
	 * 查询我是否已经申请专家
	 */
	public function is_my_apply(){
            $user_id = session('user_id');
	    $users = M('users')->where('id='.$user_id)->field('expert_type')->find();
            if($users['expert_type']==''){
                self::promptPage('对不起，请先申请成为专家类型！', U("Apply/expert_join"), false, '立马申请');
            }
	}
	
	/**
	 * 消息
	 */
	public function message() {
	    $act_count = M('activity_message')->where("user_id = {$this->user_id} and status = 1")->count();
            $apply_count = M('apply_message')->where("user_id = {$this->user_id} and status = 1")->count();
            $make_count = M('make_message')->where("user_id = {$this->user_id} and status = 1")->count();
	    $discuss_count = M('discuss')->where("reply_user_id = {$this->user_id} and status = 1")->count();
	    $this->assign('count', array($act_count, $discuss_count,$apply_count,$make_count));
//            var_dump(array($act_count, $discuss_count));
	    $this->display('message');
	}
	
	/**
	 * 活动消息
	 */
	public function activityMessage(){
	    $data = M('activity_message')->where("user_id = {$this->user_id}")->order('id desc, add_time desc')->select();
	    if(count($data) > 0){
	        $this->assign('message_show', 1);
	    }
	    $this->assign('data', $data);
	    $this->display('activity_message_list');
	}

        /**
	 * 专家审核消息
	 */
	public function applyMessage(){
	    $data = M('apply_message')->where("user_id = {$this->user_id}")->order('id desc, add_time desc')->select();
	    if(count($data) > 0){
	        $this->assign('message_show', 1);
	    }
	    $this->assign('data', $data);
	    $this->display('apply_message_list');
	}
        
         /**
	 * 预约审核消息
	 */
	public function makeMessage(){
	    $data = M('make_message')->where("user_id = {$this->user_id}")->order('id desc, add_time desc')->select();
	    if(count($data) > 0){
	        $this->assign('message_show', 1);
	    }
	    $this->assign('data', $data);
	    $this->display('make_message_list');
	}
        
	/**
	 * 讨论消息
	 */
	public function discussMessage(){
	    $data = M('discuss')->where("reply_user_id = {$this->user_id}")->order('id desc, add_time desc')->select();
	    if(count($data) > 0){
	        $this->assign('discuss_show', 1);
	    }
	    $this->assign('data', $data);
	    $this->display('discuss_message_list');
	}
	
        /**
	 * 评论消息
	 */
	public function make_discuss_list(){
	    $data = M('comment')->where("user_id = {$this->user_id} and comment_type=2")->order('id desc, add_time desc')->select();
	    if(count($data) > 0){
	        $this->assign('discuss_show', 1);
	    }
	    $this->assign('data', $data);
	    $this->display();
	}
	
	/**
	 * 活动消息详情
	 */
	public function activity_message_page(){
	    $id = I('id');
	    $status = I('status');
	    //修改状态为已读
	    if($status == 1){
	        $result = M('activity_message')->where("id = $id")->setField('status', 2);
	    }
	    $data = M('activity_message')->where("id = $id")->find();
	    $this->assign('data', $data);
	    $this->display('activity_message_page');
	}
        
        /**
	 * 专家申请消息详情
	 */
	public function apply_message_page(){
	    $id = I('id');
	    $status = I('status');
	    //修改状态为已读
	    if($status == 1){
                $map['status'] = 2;
	        $result = M('apply_message')->where("id = $id")->save($map);
	    }
	    $data = M('apply_message')->where("id = $id")->find();
	    $this->assign('data', $data);
	    $this->display();
	}
	
        /**
	 * 预约申请消息详情
	 */
	public function make_message_page(){
	    $id = I('id');
	    $status = I('status');
	    //修改状态为已读
	    if($status == 1){
                $map['status'] = 2;
	        $result = M('make_message')->where("id = $id")->save($map);
	    }
	    $data = M('make_message')->where("id = $id")->find();
	    $this->assign('data', $data);
	    $this->display();
	}
        
	/**
	 * 讨论消息详情
	 */
	public function discuss_message_page(){
	    $id = I('id');
	    $status = I('status');
	    //修改状态为已读
	    if($status == 1){
	        $result = M('discuss')->where("id = $id")->setField('status', 2);
	    }
	    $data = M('discuss')->where("id = $id")->find();
	    $data['activity_name'] = M('activity')->where("id = {$data['activity_id']}")->getField('activity_name');
	    $this->assign('data', $data);
	    $this->display('discuss_message_page');
	}
        
        /**
	 * 讨论消息详情
	 */
	public function make_discuss_page(){
	    $id = I('id');
	    $status = I('status');
	    //修改状态为已读
	    if($status == 1){
	        $result = M('discuss')->where("id = $id")->setField('status', 2);
	    }
	    $data = M('discuss')->where("id = $id")->find();
	    $data['activity_name'] = M('activity')->where("id = {$data['activity_id']}")->getField('activity_name');
	    $this->assign('data', $data);
	    $this->display();
	}
	
	/**
	 * 验票
	 * @param varchar $order_sn 订单编号
	 * @param int $sponsor_user_id 主办方id
	 */
	public function checkTicket() {
	    $order_sn = I('order_sn');
	    $sponsor_user_id = I('sponsor_user_id');
	    if($order_sn &&  $sponsor_user_id){
	        //扫码
	        $result = self::checkSweep($order_sn, $sponsor_user_id);
	        if($result){
	            //发送消息
	            $message_result = self::sendMessage($order_sn);
	            if($message_result){
	                //扫码成功，修改订单状态
	                self::editOrderStatus($order_sn);
	            }
	        }
	    }else{
	        self::promptPage('对不起，参数错误，请重新扫码！', U('Index/index'), false, '回到首页');
	    }
	}
	
	/**
	  * 扫码成，修改订单状态为已完成
	  * @param string $order_sn
	 */
	public function editOrderStatus($order_sn){
	    $activity_order_model = new ActivityOrderModel;
	    $order_status = $activity_order_model->getOrderStatus($order_sn);
	    //是否已经扫码通过的
	    if($order_status == 2){
	        //扫码后修改订单状态
	        $row = $activity_order_model->checkTicket($order_sn);
	        if($row){
	            self::promptPage('恭喜你，扫码成功', U("Users/index"), false,'用户中心');
	        }else{
	            self::promptPage('对不起，网络缓慢，请重新扫码！', U('Index/index'), false, '回到首页');
	        }
	    }else{
	        self::promptPage('该订单已被扫码通过，无效重复扫码', U('Index/index'), false, '回到首页');
	    }
	}
	
    /**
      * 发送扫码成功消息
      * @param string $order_sn
     */
	public function sendMessage($order_sn){
	    $act_data = M('activity_order')->field('activity_name, activity_id, user_id')
                                	   ->where("order_sn = '$order_sn'")
                                	   ->find();
	    $data = array('activity_id'=>$act_data['activity_id'], 'user_id'=>$act_data['user_id'],
	        'title'=>'扫码通过',
	        'content'=>'恭喜你，你参加的 "'.$act_data['activity_name'].'" 活动已经扫码通过！',
	        'auth'=>'admin', 'add_time'=>time());
	    //发送消息
	    $result = M('activity_message')->add($data);
	    if($result)
	        return true;
	    else
	        self::promptPage('发送消息失败', U('Index/index'), false, '回到首页');
	}
	
	/**
	  * 主办方扫码or扫码人员扫码
	  * @param string $order_sn
	  * @param int $sponsor_user_id
	  * @return boolean
	 */
	public function checkSweep($order_sn, $sponsor_user_id){
	    //主办方扫码
	    if($sponsor_user_id == $this->user_id){
	        $result = self::sweepLog($order_sn, '主办方');
	    //扫码人员扫码    
	    }else{
	        $result = self::sweepUser($order_sn, $sponsor_user_id);
	    }
	    if($result) return true;
	}
	
	/**
	  * 扫码人员扫码
	  * @param int $sponsor_user_id
	  * @return boolean
	 */
	public function sweepUser($order_sn, $sponsor_user_id){
	    $sweep_user_id = M('sweep')->where("sponsor_user_id = $sponsor_user_id")->getField('bind_user_id',true);
	    if(count($sweep_user_id) > 0){
	        if(in_array($this->user_id, $sweep_user_id)){
	            $result = self::sweepLog($order_sn, '扫码人员');
	            if($result) return true;
	        }else{
	            self::promptPage('对不起，你不是扫码人员，不可扫码！', U('Index/index'), false, '回到首页');
	        }
	    }else{
	        self::promptPage('对不起，你不是扫码人员，不可扫码！', U('Index/index'), false, '回到首页');
	    }
	}
	
	/**
	  * 添加扫码日志
	  * @param string $order_sn
	 */
	public function sweepLog($order_sn, $sweep_user){
	    $phone_number = M('users')->where("id = {$this->user_id}")->getField('phone_number');
	    $act_data = M("activity_order")->field('activity_id, activity_name')->where("order_sn = '$order_sn'")->find();
	    $data = array(
	        'order_sn' => $order_sn,
	        'activity_id' => $act_data['activity_id'],
	        'activity_name' => $act_data['activity_name'],
	        'user_id' => $this->user_id,
	        'head_img' => $this->headimgurl,
	        'nickname' => $this->nickname,
	        'phone_number' => $phone_number,
	        'status' => 1,
	        'sweep_user' => $sweep_user,
	        'add_time' => time()
	    );
	    $result = M('sweep_log')->add($data);
	    if($result)
	        return true;
	    else
	        self::promptPage('扫码记录添加失败！', U('Index/index'), false, '回到首页');
	}
    
	/**
	  * 提示页面
	  * @param string $message 提示内容
	  * @param string $jump_url 跳转链接
	  * @param boolean $back 返回
	  * @param string $text 按钮文字
	  * @date 2017年9月12日 - 下午2:53:47
	  * @author JiangYongWei
	 */
	public function promptPage($message, $jump_url='', $back=false, $text){
	    $this->assign('title', '提示');
	    $this->assign('message', $message);
	    $this->assign('jump_url', $jump_url);
	    $this->assign('text', $text);
	    if($back){
	        $this->assign('back', 1);
	    }
	    $this->display('Public/publish_tishi');
	    exit;
	}
	
	/**
	 * 添加扫码人员
	 */
	public function addSweep() {
	    $phone_number = I('phone_number');
	    if($phone_number){
	        $count = M('sweep')->where("phone_number = '$phone_number'")->count();
	        if($count){
	            self::promptPage('对不起，不可重复添加！', '', true, '返回');
	        }
	        $user_data = M('users')->field('id, nickname, headimgurl, phone_number')->where("phone_number = '$phone_number'")->find();
	        if(count($user_data) > 0){
	            $data = array(
	                'sponsor_user_id' => $this->user_id,
	                'bind_user_id' => $user_data['id'],
	                'nickname' => $user_data['nickname'],
	                'head_img' => $user_data['headimgurl'],
	                'phone_number' => $user_data['phone_number'],
	                'add_time' => time()
	            );
	            $row = M('sweep')->add($data);
	            if($row){
	                self::promptPage('添加扫码人员成功！', U('Users/addSweep'), false, '查看');
	            }
	        }else{
	            self::promptPage($phone_number.'手机号码不存在!', '', true, '返回');
	        }
	    }
	    $sweep_data = M('sweep')->where("sponsor_user_id = {$this->user_id}")->order('id desc')->select();
	    if(count($sweep_data) < 1){
	        $this->assign('sweep_show', 1);
	    }
	    $this->assign('sweep', $sweep_data);
	    $this->display('sweep');
	}
	
	/**
	 * 查看报名
	 */
	public function lookSignup(){
	    $activity_id = I('activity_id');
	    $info_set_model = new \Home\Model\InfoSetModel();
	    $data = $info_set_model->getmoreSignUp($activity_id);
	    if(count($data[0]) > 0){
	        $this->assign('signup_show', 1);
	    }
	    $this->assign('sign', $data[0]);
	    $this->assign('sign_count', $data[2]);
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_look_sign');
	        exit;
	    }
	    $this->display('look_sign');
	}
	
	/**
	 * 取消订单
	 */
	public function cancelOrder() {
	    $order_id = I('order_id');
	    $resutl = M('activity_order')->where("id = $order_id")->save(array('order_status'=>4));
	    if($resutl)
	       self::promptPage('取消订单成功', U('Users/my_order'), false, '返回');
	    else 
	       self::promptPage('操作失败', '', true, '返回');
	}
}